#include "SSCFETSQuote.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
// #define new DEBUG_NEW
#endif

// CFETSQUOTE
CFETSQUOTE::CFETSQUOTE() { Clear(); }

CFETSQUOTE::~CFETSQUOTE() {}

void CFETSQUOTE::Clear() {
  memset(m_sBondKey, 0, sizeof(m_sBondKey));
  // memset(m_sBondCode, 0, sizeof(m_sBondCode));
  memset(m_sListedMarket, 0, sizeof(m_sListedMarket));
  memset(m_Symbol, 0, sizeof(m_Symbol));
  memset(m_BidPriceYield, 0, sizeof(m_BidPriceYield));
  memset(m_BidPriceClean, 0, sizeof(m_BidPriceClean));
  memset(m_BidPriceFull, 0, sizeof(m_BidPriceFull));
  memset(m_BidVol, 0, sizeof(m_BidVol));
  memset(m_bidVolDescription, 0, sizeof(m_bidVolDescription));
  m_bidTime = 0;
  m_bidversion = 0;
  memset(m_OfrPriceYield, 0, sizeof(m_OfrPriceYield));
  memset(m_OfrPriceClean, 0, sizeof(m_OfrPriceClean));
  memset(m_OfrPriceFull, 0, sizeof(m_OfrPriceFull));
  memset(m_OfrVol, 0, sizeof(m_OfrVol));
  memset(m_ofrVolDescription, 0, sizeof(m_ofrVolDescription));
  m_ofrversion = 0;
  m_ofrTime = 0;

  m_time = 0;
  m_BidCompanyName = "";
  m_OfrCompanyName = "";
}

CFETSQUOTE& CFETSQUOTE::operator=(const CFETSQUOTE& one) {
  FIELDCOPYN(m_sBondKey, sizeof(m_sBondKey), one.m_sBondKey);
  // FIELDCOPYN(m_sBondCode, sizeof(m_sBondCode), one.m_sBondCode);
  FIELDCOPYN(m_sListedMarket, sizeof(m_sListedMarket), one.m_sListedMarket);
  FIELDCOPYN(m_Symbol, sizeof(m_Symbol), one.m_Symbol);
  FIELDCOPYN(m_BidPriceYield, sizeof(m_BidPriceYield), one.m_BidPriceYield);
  FIELDCOPYN(m_BidPriceClean, sizeof(m_BidPriceClean), one.m_BidPriceClean);
  FIELDCOPYN(m_BidPriceFull, sizeof(m_BidPriceFull), one.m_BidPriceFull);
  FIELDCOPYN(m_BidVol, sizeof(m_BidVol), one.m_BidVol);
  FIELDCOPYN(m_OfrPriceYield, sizeof(m_OfrPriceYield), one.m_OfrPriceYield);
  FIELDCOPYN(m_OfrPriceClean, sizeof(m_OfrPriceClean), one.m_OfrPriceClean);
  FIELDCOPYN(m_OfrPriceFull, sizeof(m_OfrPriceFull), one.m_OfrPriceFull);
  FIELDCOPYN(m_OfrVol, sizeof(m_OfrVol), one.m_OfrVol);
  FIELDCOPYN(m_bidVolDescription, sizeof(m_bidVolDescription),
             one.m_bidVolDescription);
  FIELDCOPYN(m_ofrVolDescription, sizeof(m_ofrVolDescription),
             one.m_ofrVolDescription);
  m_time = one.m_time;
  m_bidversion = one.m_bidversion;
  m_ofrversion = one.m_ofrversion;
  m_BidCompanyName = one.m_BidCompanyName;
  m_OfrCompanyName = one.m_OfrCompanyName;
  return *this;
}

//////////////////////////////////////////////////////////////////////
// class CXCQuote

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCFETSQuoteRep::CCFETSQuoteRep() {}

CCFETSQuoteRep::~CCFETSQuoteRep() { Clear(); }

bool CCFETSQuoteRep::Clear() {
  std::lock_guard<std::mutex> locker(m_lock);
  m_RepMap.m_map.clear();
  return true;
}

bool CCFETSQuoteRep::InsertQuote(CCFETSQuote::CFETSQUOTETYPE nType,
                                 const CFETSQUOTE& newElement) {
  if (nType <= CCFETSQuote::QINVALID || nType >= CCFETSQuote::QMAX)
    return false;
  std::lock_guard<std::mutex> locker(m_lock);
  auto it = m_RepMap.m_map.find(nType);
  if (it == m_RepMap.m_map.end()) {
    m_RepMap.m_map.insert(std::make_pair(nType, newElement));
    return true;
  }
  if (strcmp(newElement.m_Symbol, "1") == 0) {
    if (newElement.m_bidversion < it->second.m_bidversion)
      return false;  // 较旧版本
    it->second.m_BidCompanyName = newElement.m_BidCompanyName;
    FIELDCOPY(it->second.m_BidPriceYield, newElement.m_BidPriceYield);
    FIELDCOPY(it->second.m_BidPriceClean, newElement.m_BidPriceClean);
    FIELDCOPY(it->second.m_BidPriceFull, newElement.m_BidPriceFull);
    FIELDCOPY(it->second.m_BidVol, newElement.m_BidVol);
    FIELDCOPY(it->second.m_bidVolDescription, newElement.m_bidVolDescription);
    it->second.m_bidversion = newElement.m_bidversion;
  } else if (strcmp(newElement.m_Symbol, "-1") == 0) {
    if (newElement.m_ofrversion < it->second.m_ofrversion)
      return false;  // 较旧版本
    it->second.m_OfrCompanyName = newElement.m_OfrCompanyName;
    FIELDCOPY(it->second.m_OfrPriceYield, newElement.m_OfrPriceYield);
    FIELDCOPY(it->second.m_OfrPriceClean, newElement.m_OfrPriceClean);
    FIELDCOPY(it->second.m_OfrPriceFull, newElement.m_OfrPriceFull);
    FIELDCOPY(it->second.m_OfrVol, newElement.m_OfrVol);
    FIELDCOPY(it->second.m_ofrVolDescription, newElement.m_ofrVolDescription);
    it->second.m_ofrversion = newElement.m_ofrversion;
  } else
    return false;

  it->second.m_time = newElement.m_time;

  return true;
}

bool CCFETSQuoteRep::GetQuoteByKey(CCFETSQuote::CFETSQUOTETYPE nType,
                                   CFETSQUOTE& stRep) {
  if (nType <= CCFETSQuote::QINVALID || nType >= CCFETSQuote::QMAX)
    return false;
  std::lock_guard<std::mutex> locker(m_lock);
  if (m_RepMap.m_map.size() == 0) return false;
  auto itr = m_RepMap.m_map.find(nType);
  if (itr != m_RepMap.m_map.end()) {
    stRep = itr->second;
    return true;
  }
  return false;
}

int CCFETSQuoteRep::GetSize() const {
  std::lock_guard<std::mutex> locker(const_cast<std::mutex&>(m_lock));
  return m_RepMap.m_map.size();
}
const char* CCFETSQuoteRep::GetName() const { return "CCFETSQuoteRep"; }
int CCFETSQuoteRep::GetBytes(int mode) const {
  std::lock_guard<std::mutex> locker(const_cast<std::mutex&>(m_lock));
  ;
  int bytes =
      sizeof(std::map<CCFETSQuote::CFETSQUOTETYPE, CFETSQUOTE>::value_type) *
      m_RepMap.m_map.size();
  bytes += sizeof(*this);
  return bytes;
}

//////////////////////////////////////////////////////////////////////
// CCFETSDealQuoteRep
//////////////////////////////////////////////////////////////////////

CCFETSDealQuoteRep::CCFETSDealQuoteRep() {}

CCFETSDealQuoteRep::~CCFETSDealQuoteRep() { Clear(); }

bool CCFETSDealQuoteRep::Clear() {
  std::lock_guard<std::mutex> locker(m_lock);
  m_RepMap.m_map.clear();
  return true;
}

bool CCFETSDealQuoteRep::InsertDealQuote(
    const xQBCFETSDealAllUnit& newElement) {
  std::lock_guard<std::mutex> locker(m_lock);
  auto it = m_RepMap.m_map.find(1);
  if (it == m_RepMap.m_map.end())
    m_RepMap.m_map[1] = newElement;
  else if (it->second.m_version < newElement.m_version)
    m_RepMap.m_map[1] = newElement;
  return true;
}

bool CCFETSDealQuoteRep::GetDealQuote(xQBCFETSDealAllUnit& stRep) {
  memset(&stRep, 0, sizeof(xQBCFETSDealAllUnit));
  std::lock_guard<std::mutex> locker(m_lock);
  auto it = m_RepMap.m_map.find(1);
  if (it == m_RepMap.m_map.end()) return false;
  memcpy(&stRep, &(it->second), sizeof(xQBCFETSDealAllUnit));
  return true;
}
int CCFETSDealQuoteRep::GetSize() const {
  std::lock_guard<std::mutex> locker(const_cast<std::mutex&>(m_lock));
  return m_RepMap.m_map.size();
}
int CCFETSDealQuoteRep::GetBytes(int mode) const {
  std::lock_guard<std::mutex> locker(const_cast<std::mutex&>(m_lock));
  int bytes = sizeof(std::map<int, xQBCFETSDealAllUnit>::value_type) *
              m_RepMap.m_map.size();
  bytes += sizeof(*this);
  return bytes;
}
const char* CCFETSDealQuoteRep::GetName() const { return "CCFETSDealQuoteRep"; }
int CCFETSDealQuoteRep::GetItemSize() const {
  return sizeof(xQBCFETSDealAllUnit);
}
